﻿using System.Data.SqlClient;
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PDO.DataPersistence.ServiceProvider
{
    public class DatabaseConnectionBuilder
    {
        #region Constants

        // Default config file key into a connectionStrings section
        const string DefaultConfigurationKey = "PDO.FX.Properties.Settings.ServerConnection";

        // Default values used to construct connnectionstring.
        // Use this information to construct a connectionstring completely in 
        // code, so no security sensitive info needs to be present in config file.
        const string DefaultUser = "pdoUser";
        const string DefaultPassword = "<somethingObscure>";
        const string DefaultDataSource = "localhost";
        const string DefaultCatalog = "Northwind";

        #endregion

        #region Public Static Methods

        /// <summary>
        /// Build up a valid SQL connection string starting from connnection string "skeleton" which may be in config file.
        /// This may be a complete connection string, or portions such as User ID and Password may be ommitted for security reasons.
        /// Missing elements are detected and defaults provided as necessary to build up a complete connectionstring.
        /// </summary>
        /// <param name="connectionStringKey"></param>
        /// <returns></returns>
        public static string GetConnectionString(string configurationKey)
        {

            string skeletonConnectionString = string.Empty;
            try
            {
                // Get connnection string "skeleton" from config file.
                ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[configurationKey];
                if (null != settings)
                {
                    skeletonConnectionString = settings.ConnectionString;
                }
            }
            catch (ConfigurationErrorsException)
            {
                // If we can't get the connection string from the config, we'll make our own from scratch.
            }

            // Load and parse connectionstring skeleton.
            SqlConnectionStringBuilder scb = new SqlConnectionStringBuilder(skeletonConnectionString);

            if (scb.IntegratedSecurity == false)
            {
                if (string.Empty == scb.UserID)
                {
                    scb.UserID = DefaultUser;
                }

                if (string.Empty == scb.Password)
                {
                    scb.Password = DefaultPassword;
                }
            }

            if (string.Empty == scb.DataSource)
            {
                scb.DataSource = DefaultDataSource;
                scb.Pooling = false;
            }

            if (string.Empty == scb.InitialCatalog)
            {
                scb.InitialCatalog = DefaultCatalog;
            }

            return scb.ToString();
        }


        public static string GetConnectionString()
        {
            return GetConnectionString(DefaultConfigurationKey);
        }

        #endregion

    }
}
